<!-- 登录页面 -->
<template>
  <div class="login">
    <h1>Edu boss管理系统</h1>
    <el-form
      :model="loginForm"
      :rules="rules"
      ref="loginForm"
      label-position="top"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="loginForm.phone"></el-input>
      </el-form-item>
       <el-form-item label="密码"  prop="password">
        <el-input type="password" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onLogin()" element-loading-spinner="el-icon-loading"  :loading="loading">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 引入 登录接口
import { login } from '@/services/users'
export default {
  name: 'LoginIndex',
  data () {
    return {
      loginForm: {
        phone: '18201288771',
        password: '111111'
      },
      loading: false,
      // 表单验证规则
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3|4|5|7|8][0-9]{9}$/, message: '请输入正确手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '密码范围长度6-18', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async onLogin () {
      try {
        // 1.表单验证
        await this.$refs.loginForm.validate()
        this.loading = true
        // 2.请求
        const { data } = await login(this.loginForm)
        this.loading = false
        // 3.响应处理
        if (data.state === 1) {
        // 登录成功, 存储用户登录状态信息, 存储到vuex
          this.$store.commit('setUser', data.content)
          // 登录成功后, 跳转到导航守卫处所设定的重定向路由上去
          this.$router.push(this.$route.query.redirect || '/')
          this.$message.success(data.message)
        } else {
          this.$message.error(data.message)
        }
      } catch (err) {
        console.log('请求错误', err)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100vh;
  margin: 0 auto;
  max-width: 352px;
  h1 {
    margin: 72px auto 56px;
    width: 315px;
    text-align: center;
    cursor: pointer;
    color: rgba(0, 0, 0, 0.5);
    font-size: 48px;
    font-weight: 300;
    &:hover {
    text-shadow: 0 0 0.5rem rgb(0 0 0 / 20%);
    }
  }
  .el-form{
    padding: 30px 20px;
    background-color: #fff;
    .el-button{
      width: 100%;
    }
  }
}
</style>
